package net.sxjr.protal.timer;

import lombok.extern.slf4j.Slf4j;
import net.sxjr.mbg.entity.ZhfxYj;
import net.sxjr.protal.mapper.ZhfxMapper;
import net.sxjr.protal.mapper.ZhfxXiuFuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.EmptyStackException;

@Slf4j
@Component
public class ZhfxXiuFuTimer {
    @Autowired
    private ZhfxXiuFuMapper zhfxMapper;
    @Scheduled(cron = "0 30 23 17 4 *")
    public void zhfx1(){
        log.warn("数据补偿定时器启动");
        Calendar instance = Calendar.getInstance();
        int year = instance.get(Calendar.YEAR);
        int m = instance.get(Calendar.MONTH);
        if(year == 2021 && m==3) {
            log.error("数据补偿定时器启动执行");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

            //获取当前时间

            for (int month = 1; month < 4; month++) {
                Calendar c = Calendar.getInstance();
                c.set(Calendar.MONTH, month - 1);

                int maximum = c.getActualMaximum(Calendar.DAY_OF_MONTH);


                ZhfxYj zhfxYj = new ZhfxYj();
                zhfxYj.setMonth(sdf.format(c.getTime()));


                String start = "2021-0" + month + "-01";
                String end = "2021-0" + month + "-" + maximum;

                Integer wdjdhgs = zhfxMapper.wdjdhgs(start, end);
                zhfxYj.setPingjiaHege(wdjdhgs);
                zhfxYj.setPingjiaBuhege(zhfxMapper.wdjdzs(start, end) - wdjdhgs);

                //预警人数
                Integer currentMonthsqyjrs = zhfxMapper.currentMonthsqyjrs(start, end);
                Integer currentMonthszjdrs = zhfxMapper.currentMonthszjdrs(start, end);
                Integer currentMonthshpjrs = zhfxMapper.currentMonthshpjrs(start, end);
                zhfxYj.setYujingSq(currentMonthsqyjrs);
                zhfxYj.setYujingSz(currentMonthszjdrs);
                zhfxYj.setYujingSh(currentMonthshpjrs);
                zhfxYj.setPersonNum(currentMonthsqyjrs + currentMonthszjdrs + currentMonthshpjrs);
                //三重一大
                //本月合格次数
                Integer szydhgs = zhfxMapper.szydhgs(start, end);
                zhfxYj.setPingjiaHegeSzyd(szydhgs);
                zhfxYj.setPingjiaBuhegeSzyd(zhfxMapper.szydzs(start, end) - szydhgs);


                zhfxYj.setYujingSqSzyd(zhfxMapper.sqyjrsszyd(start, end));
                zhfxYj.setYujingSzSzyd(zhfxMapper.szjdrsszyd(start, end));
                zhfxYj.setYujingShSzyd(zhfxMapper.shpjrszdyd(start, end));


                //项目管理
                //本月合格次数
                Integer xmjdhgs = zhfxMapper.xmjdhgs(start, end);
                zhfxYj.setPingjiaHegeXmgl(xmjdhgs);
                zhfxYj.setPingjiaBuhegeXmgl(zhfxMapper.xmjdzs(start, end) - xmjdhgs);

                zhfxYj.setYujingSqXmgl(zhfxMapper.sqyjrsxmjd(start, end));
                zhfxYj.setYujingSzXmgl(zhfxMapper.szjdrsxmgl(start, end));
                zhfxYj.setYujingShXmgl(zhfxMapper.shpjrsxmgl(start, end));

                //薪酬管理
                Integer xcgljdhgs = zhfxMapper.xcgljdhgs(start, end);
                zhfxYj.setPingjiaHegeXcgl(xcgljdhgs);
                zhfxYj.setPingjiaBuhegeXcgl(zhfxMapper.xcgljdzs(start, end) - xcgljdhgs);

                zhfxYj.setYujingSqXcgl(zhfxMapper.sqyjrsxcgl(start, end));
                zhfxYj.setYujingSzXcgl(zhfxMapper.szjdrsxcgl(start, end));
                zhfxYj.setYujingShXcgl(zhfxMapper.shpjrsxcgl(start, end));
                log.error("薪酬管理结束{}",month);
                //干部管理
                Integer gbjdglhgs = zhfxMapper.gbjdglhgs(start, end);
                zhfxYj.setPingjiaHegeGbgl(gbjdglhgs);
                zhfxYj.setPingjiaBuhegeGbgl(zhfxMapper.gbjdglzs(start, end) - gbjdglhgs);

                zhfxYj.setYujingSqGbgl(zhfxMapper.sqyjrsgbgl(start, end));
                zhfxYj.setYujingSzGbgl(zhfxMapper.szjdrssgbgl(start, end));
                zhfxYj.setYujingShGbgl(zhfxMapper.shpjrsgbgl(start, end));

                //项目管理 项目金额
                zhfxYj.setXmglxmje(zhfxMapper.xmglxiangmujiee(start, end));


                zhfxYj.insert();

            }
        }
    }

}
